@page "/login"
@using System.ComponentModel.DataAnnotations
@using BlazorWasmAuth.Identity
@using BlazorWasmAuth.Identity.Models
@inject IAccountManagement Acct
@inject NavigationManager Navigation

<PageTitle>Login</PageTitle>

<h1>Login</h1>

<AuthorizeView>
    <Authorized>
        <div class="alert alert-success">You're logged in as @context.User.Identity?.Name.</div>
    </Authorized>
    <NotAuthorized>
        @foreach (var error in formResult.ErrorList)
        {
            <div class="alert alert-danger">@error</div>
        }
        <div class="row">
            <div class="col">
                <section>
                    <EditForm Model="Input" method="post" OnValidSubmit="LoginUser"
                              FormName="login" Context="editform_context">
                        <DataAnnotationsValidator />
                        <h2>Use a local account to log in.</h2>
                        <hr />
                        <div class="form-floating mb-3">
                            <InputText @bind-Value="Input.Email"
                                       id="Input.Email"
                                       class="form-control"
                                       autocomplete="username"
                                       aria-required="true"
                                       placeholder="name@example.com" />
                            <label for="Input.Email" class="form-label">
                                Email
                            </label>
                            <ValidationMessage For="() => Input.Email"
                                               class="text-danger" />
                        </div>
                        <div class="form-floating mb-3">
                            <InputText type="password"
                                       @bind-Value="Input.Password"
                                       id="Input.Password"
                                       class="form-control"
                                       autocomplete="current-password"
                                       aria-required="true"
                                       placeholder="password" />
                            <label for="Input.Password" class="form-label">
                                Password
                            </label>
                            <ValidationMessage For="() => Input.Password"
                                               class="text-danger" />
                        </div>
                        <div>
                            <button type="submit" class="w-100 btn btn-lg btn-primary">
                                Log in
                            </button>
                        </div>
                        <div class="mt-3">
                            <p>
                                <a href="register">Register as a new user</a>
                            </p>
                        </div>
                    </EditForm>
                </section>
            </div>
        </div>
    </NotAuthorized>
</AuthorizeView>

@code {
    private FormResult formResult = new();

    [SupplyParameterFromForm]
    private InputModel Input { get; set; } = new();

    [SupplyParameterFromQuery]
    private string? ReturnUrl { get; set; }

    public async Task LoginUser()
    {
        formResult = await Acct.LoginAsync(Input.Email, Input.Password);

        if (formResult.Succeeded && !string.IsNullOrEmpty(ReturnUrl))
        {
            Navigation.NavigateTo(ReturnUrl);
        }
    }

    private sealed class InputModel
    {
        [Required]
        [EmailAddress]
        [Display(Name = "Email")]
        public string Email { get; set; } = string.Empty;

        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; } = string.Empty;
    }
}
